// pages/danger/serivce/serivce.js
const app = getApp()
const host = app.globalData.host
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioGroup:[],
     pageInfo:[
       {
         "title": "收费",
         "id": 1,
         "list_order": 0,
          "sub_type": 0,
          "sub_data": [
            {
            "id":1,
            "list_order":0,
            "title":"未成立业主委员会的经济适用住房小区未执行政府指导价的"
            },
            {
              "id": 2,
              "list_order": 0,
              "title":"其他价格违法行为"
            },
            {
              "id": 3,
              "list_order": 0,
              "title": "违规收费"
            }
         ]
       }
     ],
     questions:[],
    
     checked:false,
     checkeds:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    var token = wx.getStorageSync('token')
    var id = options.id;
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    //主页内容
    wx.request({
      url: host + '/risk/subs',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: token,
        id:id
      },
      success(res) {
        var data = [];
        wx.hideLoading()
        console.log(res.data);
        for (var i in that.data.pageInfo.data){
          console.log(that.data.pageInfo[i].sub_data);
          for (var i in that.data.pageInfo[i].sub_data){
            data.push({
              isChecked:false,
              isNotChecked:false
            })
          }
        }
        console.log(data);
        if (res.data.code == 0) {
          that.setData({
           // pageInfo: res.data.data,
            radioGroup:data
          })
          
        } else {
          app.globalData.isShow(res.data.msg);
        }

      },
      fail(res) {
        var msg = res.data.msg;
        app.globalData.isShow(res.data.msg);
      }
    })
  },

  radioChange:function(e){
    console.log(e)
   var that = this;
    var that = this;
    if(e.detail.value == "Y"){
      
      that.setData({
        questions: that.data.questions.concat(e.currentTarget.dataset.id)
      })
      
    }
  },
  //获取错误文件
  getDocument(mq){
    var token = wx.getStorageSync('token')
    wx.request({
      url: host + '/files/mistakefiles',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        mq_list:mq,
        token:token
      },
      success(res){
        console.log(res);
      },
      fail(res){
        app.globalData.isShow(res.data.msg)
      }
    })
  },
  //取消选中
  del(e){
    var that = this;
    var data = this.data.radioGroup
    console.log(e.currentTarget.dataset.index);
    console.log(e.currentTarget.dataset.type);
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type
    for(var i in data){
      if(index == i){
        if (type == 0) {
          that.data.radioGroup[index].isChecked = !that.data.radioGroup[index].isChecked;
          that.data.radioGroup[index].isNotChecked = false;
        }
        if (type == 1) {
          that.data.radioGroup[index].isChecked = false;
          that.data.radioGroup[index].isNotChecked = !that.data.radioGroup[index].isNotChecked;
        }
      }
    }
    that.setData({
      radioGroup:that.data.radioGroup
    })
    console.log(this.data.radioGroup); 
  },
  formSubmit(e){
    var data = []
    for (let i in that.data.questions){
      if (data.indexOf(that.data.questions[i]) == -1){
        data.push(that.data.questions[i]);
      }
    }
    console.log(data);
    var token = wx.getStorageSync('token')
    var that = this;
    console.log(this.data.questions);
    wx.request({
      url: host+'/risk/subs',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: token,
        skip_ques: data
      },
      success(res) {
        wx.hideLoading()
        console.log(res.data);
        if (res.data.code == 0) {
          if(that.data.questions.length == 0){
            wx.navigateTo({
              url: '../feedback/feedback',
            })
          }else{
            that.getDocument(that.data.questions);
          }  
        } else {
          app.globalData.isShow(res.data.msg)
        }

      },
      fail(res) {
        app.globalData.isShow(res.data.msg)
      }
    })
  },
  
})